<?php

class Admin_model extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->tableAdminInfoName = "admin_user";
        $this->tableAdminRoleName = "admin_role";
        $this->tableAdminPowerName = "admin_power";
    }

    /*
     * 得取权取表数据
     */

    /*
     * 得到角色
     */

    public function getAdminPower($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableAdminPowerName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 添加角色
     */

    public function addAdminRole($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableAdminRoleName, $data);
        }
        return $outArr;
    }

    /*
     * 编辑角色
     */

    public function updateAdminRole($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableAdminRoleName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除角色
     */

    public function delAdminRole($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableAdminRoleName, $whereArr);
        return $outArr;
    }

    /*
     * 得到角色
     */

    public function getAdminRole($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableAdminRoleName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 根据ID得到用户信息
     */

    function getAdminRoleById($id) {
        $outArr = array();
        $outArr = $this->getAdminRole("WHERE id=" . $id, 1, 0);
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    /*
     * 得到角色名KEY值为ID
     */

    public function getAdminRoleByKey() {
        $result = array();
        $sql = "SELECT id,name FROM " . $this->tableAdminRoleName;
        $query = $this->db->query($sql);
        $result = $query->result_array();
        if ($result) {
            $resultTmp = array();
            foreach ($result as $k => $v) {
                $resultTmp[$v["id"]] = $v;
            }
            $result = $resultTmp;
        }
        return $result;
    }

    /*
     * 得到条数
     */

    function getAdminRoleCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableAdminRoleName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    /*
     * 添加用户
     */

    public function addAdminInfo($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableAdminInfoName, $data);
        }
        return $outArr;
    }

    /*
     * 编辑用户
     */

    public function updateAdminInfo($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableAdminInfoName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除用户
     */

    public function delAdminInfo($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableAdminInfoName, $whereArr);
        return $outArr;
    }

    /*
     * 得到角色
     */

    public function getAdminInfo($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableAdminInfoName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到用户条数
     */

    function getAdminInfoCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableAdminInfoName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    /*
     * 验证用户登录
     */

    function checkAdminLogin($username, $password) {

        $sql = "SELECT * FROM " . $this->tableAdminInfoName . " WHERE status_id=1 AND username='$username' AND password='$password'";
        //echo $sql;
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result;
    }

    /*
     * 根据用户名查找数据库是否存在记录
     */

    function getAdminByUsername($username, $adminId) {
        $outArr = array();
        $outArr = $this->getAdminInfo("WHERE status_id=1 AND username='" . $username . "'" . ($adminId > 0 ? " AND id!=" . $adminId : ""));
        return $outArr;
    }

    /*
     * 根据ID得到用户信息
     */

    function getAdminInfoById($adminId) {
        $outArr = array();
        $outArr = $this->getAdminInfo("WHERE id=" . $adminId, 1, 0, "");
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

}
